package com.fang.stream_export_date.server.impl;

import com.fang.stream_export_date.handler.CustomResultHandler;
import com.fang.stream_export_date.mapper.TestExportMapper;
import com.fang.stream_export_date.utils.DownloadProcessor;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2023/10/19 23:15
 **/
@Service
public class DownloadServiceImpl {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Autowired
    private TestExportMapper testExportMapper;

    /**
     * stream读数据写文件方式
     *
     * @param httpServletResponse
     * @throws IOException
     */
    public void streamDownload(HttpServletResponse httpServletResponse) throws IOException {
        CustomResultHandler customResultHandler = new CustomResultHandler(new DownloadProcessor(httpServletResponse));
        // TODO: need to select method
        sqlSessionTemplate.select(
                "com.fang.stream_export_date.mapper.TestExportMapper.streamByExample", null, customResultHandler);
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }

}
